#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Sequence Item Database Toolkit

Mikan Python Web Framework
"""

__author__ = "K.Sonohara"
__status__ = "develop"
__version__ = "0.0.0_36"
__date__    = "2014/01/05"
__license__ = 'LGPL'

# ------------------------------------------------------------
## インポート
# ------------------------------------------------------------
# Python

# ESiPC
from esipc.resource.iniresource import ESiPCINIFileResourceBundle

# Mikan
from mikan.idt import AbstractMikanItemDBToolkit
from mikan.exception import MikanStatusException
from mikan.core import MIKAN

# ------------------------------------------------------------
## 変数
# ------------------------------------------------------------

# ------------------------------------------------------------
## 関数
# ------------------------------------------------------------

# ------------------------------------------------------------
## クラス
# ------------------------------------------------------------
class MIKAN_IDT_REVISON(MIKAN):
	pass

class AbstractMikanSequenceIDT(AbstractMikanItemDBToolkit, MIKAN_IDT_REVISON):
	def __init__(self, itemdb):
		super(AbstractMikanSequenceIDT, self).__init__(itemdb)

class MikanSequenceIDT(AbstractMikanSequenceIDT):
	def __init__(self, itemdb):
		super(MikanSequenceIDT, self).__init__(itemdb)

	def sequence_remove(self, entity):
		if (entity.issequenceentity):
			if (entity.getLastSequenceValue()):
				entity.setLastSequenceValue(False)
				entity.setEffectiveSequenceValue(False)
				self.itemdb.update(entity)
			else:
				entity.setEffectiveSequenceValue(False)
				self.itemdb.update(entity)

	def get_sequence_master(self, entity):
		if (entity.issequenceentity):
			return self.itemdb.get(entity.getMasterClass(), entity.getMasterIDValue())

		return None

	def get_sequence_all(self, entity):
		if (entity.issequenceentity):
			where = {}
			where[entity.getMasterIDAttribute().definename] = entity.getMasterIDValue()
			return self.itemdb.gets(entity.__class__, where)

		return None

	def get_sequence_last(self, entity):
		if (entity.issequenceentity):
			where = {}
			where[entity.getMasterIDAttribute().definename] = entity.getMasterIDValue()
			where[entity.getLastSequenceAttribute().definename] = True

			result = self.itemdb.gets(entity.__class__, where)
			if len(result) > 0:
				return result[0]

		return None

	def get_sequence_next(self, entity):
		if (entity.issequenceentity):
			where = {}
			where[entity.getMasterIDAttribute().definename] = entity.getMasterIDValue()
			where[entity.getRootIDAttribute().definename] = entity.getIDValue()

			result = self.itemdb.gets(entity.__class__, where)
			if len(result) > 0:
				return result[0]

		return None

	def get_sequence_previous(self, entity):
		if (entity.issequenceentity):
			if entity.getRootIDValue() and (entity.getRootIDValue() != -1):
				return self.itemdb.get(entity.__class__, entity.getRootIDValue())

		return None

	def sequence_up(self, entity, values=None):
		if (entity.issequenceentity):
			if (entity.getLastSequenceValue()):
				e = self.itemdb.get(entity.__class__, entity.id)
				e.setLastSequenceValue(False)
				self.itemdb.update(e)

				e = entity.__class__(entity.export_values())
				if values:
					e.values.update(values)
				self.itemdb.create(e, True)
				return e
			else:
				raise MikanStatusException(self.request, 403)
		return None

	def get_sequence_value_default(self, entity):
		return entity.get_sequence_value_default()

	def get_sequence_format(self, entity):
		return entity.sequenceformat

	def get_sequence_value(self, entity, value=None):
		if value:
			try:
				v = int(value) + 1
			except:
				pass
		else:
			v = self.get_sequence_value_default(entity)
		return self.get_sequence_format(entity) % v

	def get_sequence_value_first(self, entity):
		return self.get_sequence_value(entity)

	def get_sequence_value_next(self, entity, value):
		return self.get_sequence_value(entity, value)

# ------------------------------------------------------------
## メイン関数
# ------------------------------------------------------------
if __name__ == '__main__':
	pass

# ------------------------------------------------------------
